#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //网络链接部分，响应服务器
    clientSocket =new QTcpSocket(this);
    clientSocket->connectToHost("192.168.4.1",8888);
    connect(clientSocket,&QTcpSocket::connected,this,&MainWindow::onConnetcted);
    connect(clientSocket,&QTcpSocket::readyRead,this,&MainWindow::Read_message);
    //table_view 的模型填充更新

    //connect(clientSocket,&QTcpSocket::readyRead,this,&MainWindow::onRead);
    // connect(serialPort, SIGNAL(readyRead()), this, SLOT(serialPortReadyRead_Slot()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::Read_message()
{
    QString message;
    message=clientSocket->readAll();
   // qDebug()<<message;
    QStringList numbers = message.split(":");

    if (numbers.size() ==3) {
    QString num1 = numbers.at(0); //湿度
    QString num2 = numbers.at(1); //温度
    QString num3 = numbers.at(2); //光照
   // ui->lineEdit->clear();
    ui->lineEdit->setText(num2);
  //  ui->lineEdit_2->clear();
    ui->lineEdit_2->setText(num1);
 //   ui->lineEdit_3->clear();
    ui->lineEdit_3->setText(num3);

}
}
void MainWindow::on_pushButton_clicked()
{
    clientSocket->write("~",1);
}
void MainWindow::onConnetcted()
{
    qDebug()<<"链接成功"<<endl;
}

void MainWindow::on_pushButton_2_clicked()
{
    clientSocket->write("^",1);
}

void MainWindow::on_pushButton_3_clicked()
{
    clientSocket->write("&",1);
}

void MainWindow::on_pushButton_4_clicked()
{
    clientSocket->write("*",1);
}

void MainWindow::on_pushButton_5_clicked()
{
    clientSocket->write("#",1);
}

void MainWindow::on_pushButton_6_clicked()
{
    clientSocket->write("G",1);
}
